/*
 * $Id: ProfileController.java 46 2013-05-03 21:38:12Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 46 $
 * $Date: 2013-05-03 21:38:12 +0000 (Fri, 03 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.controller;

import com.sci.logisticsmap.service.UserService;
import com.sci.logisticsmap.support.validator.CompoundValidator;
import com.sci.logisticsmap.service.model.SessionUserDTModel;
import com.sci.logisticsmap.service.model.ProfileDTCommand;
import com.sci.logisticsmap.web.security.support.SecurityContextHelper;
import com.sci.logisticsmap.web.model.ProfileVWCommand;
import com.sci.logisticsmap.web.support.validator.ProfileVWCommandValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.PostConstruct;
import javax.validation.Valid;

/**
 * The Controller for processing profile view/ajax requests.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 46 $
 */
@Controller
@RequestMapping("")
public class ProfileController extends BaseController {
    /**
     * The profile path.
     */
    public static final String PATH_PROFILE = "/profile";

    /**
     * The profile view.
     */
    private static final String viewProfile = "profilePage";

    /**
     * The user service.
     */
    @Autowired
    private UserService userService;

    /**
     * The default constructor.
     * Initializes a Controller class.
     */
    public ProfileController() {
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseController#init()
     */
    @PostConstruct
    public void init() {
        super.init();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.web.bind.annotation.InitBinder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new CompoundValidator(new ProfileVWCommandValidator()));
    }

    /**
     * The method to response on the profile view request.
     *
     * @param  model
     *         The model.
     *
     * @return the profile view.
     */
    @RequestMapping(value=PATH_PROFILE, method={RequestMethod.GET})
    public String onProfile(Model model) {
        try {
            SessionUserDTModel sessionUser = SecurityContextHelper.getAuthenticatedUserDTModel();
            ProfileDTCommand profileCommand = userService.getProfileDTCommand(sessionUser.getUsername());
            model.addAttribute("profileCommand", conversionUtilities.convert(profileCommand, ProfileVWCommand.class));
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewProfile;
    }

    /**
     * The method to response on the profile view submitting request.
     *
     * @param  command
     *         The command.
     * @param  result
     *         The validation binding result.
     * @param  model
     *         The model.
     *
     * @return the profile view.
     */
    @RequestMapping(value=PATH_PROFILE, method={RequestMethod.POST})
    public String onProfileSubmit(@ModelAttribute("profileCommand") @Valid ProfileVWCommand command, BindingResult result, Model model) {
        try {
            boolean failed = false;
            if (!result.hasErrors()) {
                try {
                    SessionUserDTModel sessionUser = SecurityContextHelper.getAuthenticatedUserDTModel();
                    userService.editProfile(sessionUser, conversionUtilities.convert(command, ProfileDTCommand.class));

                    return buildRedirectToPath(PATH_BASE);
                } catch (Exception e) {
                    addExceptionToModel(model, e);
                    failed = true;
                }
            } else {
                failed = true;
            }
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewProfile;
    }
}
